package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Consume;
import com.szxy.hotel.entity.PageBean;
import com.szxy.hotel.service.StatementService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StatementController {
    @Resource(name = "statementService")
    StatementService statementService;

    /**
     * 报表管理列表
     */
    @RequestMapping("/statementList.do")
    @ResponseBody
    public Map<String,Object> statementList(int currentPage, String consumeDate, String consumeMonth, String consumeYear, Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Consume> pageBean = new PageBean<Consume>();
        int pageSize = 8;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        if((consumeDate==null||consumeDate=="")&&(consumeMonth==null||consumeMonth=="")&&(consumeYear==null||consumeYear=="")){
            List<Consume> lists = statementService.findByPage(start, size);
            List<Consume> list = statementService.selectCount();
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "暂无消费");
                map.put("result", null);
            }else {
                Double totalMoney = 0.0;
                for (Consume consume : list){
                    totalMoney += (consume.getConsumePrice()*consume.getConsumeNumber()*consume.getVip().getVipDisDouble());
                }
                model.addAttribute("totalMoney",totalMoney);
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else if((consumeDate!=null||consumeDate!="")&&(consumeMonth==null||consumeMonth=="")&&(consumeYear==null||consumeYear=="")){
            List<Consume> lists = statementService.findByPageDate(start, size,consumeDate);
            List<Consume> list = statementService.selectCountDate(consumeDate);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "暂无消费");
                map.put("result", null);
            }else {
                Double totalMoney = 0.0;
                for (Consume consume : list){
                    totalMoney += (consume.getConsumePrice()*consume.getConsumeNumber()*consume.getVip().getVipDisDouble());
                }
                model.addAttribute("totalMoney",totalMoney);
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else if((consumeDate==null||consumeDate=="")&&(consumeMonth!=null||consumeMonth!="")&&(consumeYear==null||consumeYear=="")){
            String[] ms = consumeMonth.split("-");
            String my = ms[0];
            String mm = ms[1];
            List<Consume> lists = statementService.findByPageMonth(start, size,my,mm);
            List<Consume> list = statementService.selectCountMonth(my,mm);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "暂无消费");
                map.put("result", null);
            }else {
                Double totalMoney = 0.0;
                for (Consume consume : list){
                    totalMoney += (consume.getConsumePrice()*consume.getConsumeNumber()*consume.getVip().getVipDisDouble());
                }
                model.addAttribute("totalMoney",totalMoney);
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else if((consumeDate==null||consumeDate=="")&&(consumeMonth==null||consumeMonth=="")&&(consumeYear!=null||consumeYear!="")){
            String[] ys = consumeYear.split("-");
            String yy = ys[0];
            List<Consume> lists = statementService.findByPageYear(start, size,yy);
            List<Consume> list = statementService.selectCountYear(yy);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "暂无消费");
                map.put("result", null);
            }else {
                Double totalMoney = 0.0;
                for (Consume consume : list){
                    totalMoney += (consume.getConsumePrice()*consume.getConsumeNumber()*consume.getVip().getVipDisDouble());
                }
                model.addAttribute("totalMoney",totalMoney);
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else {
            map.put("status", 1);
            map.put("message", "数据错误");
            map.put("result", null);
        }
        return map;
    }
}
